package com.fht.controller.member;


import com.fht.entity.MemberCategory;
import com.fht.entity.MemberManager;
import com.fht.entity.MerchantsInformation;
import com.fht.from.MemberFrom;
import com.fht.service.food.MerchantsInformationService;
import com.fht.service.member.MemberCategoryService;
import com.fht.service.member.MemberManagerService;
import com.fht.util.DateTimeUtil;
import com.fht.util.ResultVOUtil;
import com.fht.vo.LayUITableVO;
import com.fht.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Slf4j
@RequestMapping("/member/manage")
public class MemberManagerController {


    @Autowired
    public MemberManagerService memberManagerService;

    @Autowired
    public MemberCategoryService memberCategoryService;

    @Autowired
    public MerchantsInformationService merchantsInformationService;

    //菜品类别展示
    @GetMapping("/index")
    public String index(Model model){

        List<MerchantsInformation> merchantsInformationList = merchantsInformationService.findAllIdAndName();
        model.addAttribute("merchantsInformationList",merchantsInformationList);

        return "member/manager/index";

    }


    //获取菜品类别信息
    @GetMapping("/list")
    @ResponseBody
    public LayUITableVO<List<MemberManager>> list (@RequestParam(defaultValue = "1") Integer page,
                                                   @RequestParam(defaultValue = "10") Integer limit,
                                                   MemberFrom from){
        Map<String ,Object> map = new HashMap<>();
        map.put("page",page);
        map.put("limit",limit);
        map.put("from",from);

        LayUITableVO<List<MemberManager>> vo = memberManagerService.findPage(map);


        return vo;
    }

    //跳转到修改页面
    @GetMapping("/edit-ui")
    public String editUI(String id,Model model){

        MemberManager memberManager= memberManagerService.selectById(id);
        List<MemberCategory> memberCategoryList = memberCategoryService.selectAll();

        model.addAttribute("memberManager",memberManager);
        model.addAttribute("memberCategoryList",memberCategoryList);

        return "member/manager/edit";

    }


    //修改map缓存中的数据，并重新计算价格总和
    @PostMapping("/edit")
    @ResponseBody
    public ResultVO edit (MemberManager memberManager){

        memberManagerService.update(memberManager);

        return ResultVOUtil.ok();
    }

    //跳转到修改页面
    @GetMapping("/add-ui")
    public String addUI(String merchantsId,Model model){

        model.addAttribute("merchantsId",merchantsId);
        List<MemberCategory> memberCategoryList = memberCategoryService.selectAll();
        model.addAttribute("memberCategoryList",memberCategoryList);
        return "member/manager/add";

    }


    //修改map缓存中的数据，并重新计算价格总和
    @PostMapping("/add")
    @ResponseBody
    public ResultVO add ( MemberManager memberManager){

        memberManager.setIntegral(0);
        memberManager.setConsumptionTotal(0);

        memberManager.setOpenCardDate(DateTimeUtil.nowDate());

        memberManagerService.save(memberManager);

        return ResultVOUtil.ok();
    }

    //修改map缓存中的数据，并重新计算价格总和
    @PostMapping("/del")
    @ResponseBody
    public ResultVO del ( String id){

        memberManagerService.deleteById(id);

        return ResultVOUtil.ok();
    }

}
